//
//  SpecialCell.swift
//  KaMengApplication
//
//  Created by DUONIU_MAC on 2018/9/6.
//  Copyright © 2018年 DUONIU_MAC. All rights reserved.
//

import UIKit

class SpecialCell: UITableViewCell {

    let titleImageView = UIImageView()
    let titleLabel = UILabel()
    let timeLabel = UILabel()
    let contextLabel = UILabel()
    let button = UIButton()
    
    var model : SpecialModel! {
        
        didSet{
            updateData()
        }
    }
    
    override init(style: UITableViewCellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        
        self.contentView.backgroundColor = UIColor.white
        
        titleImageView.image = UIImage.imageWithName("loading")
        self.addSubview(titleImageView)
        
        titleLabel.font = UIFont.systemFont(ofSize: RATIO(maxNum: 16))
        self.contentView.addSubview(titleLabel)
        
        timeLabel.font = UIFont.systemFont(ofSize: RATIO(maxNum: 14))
        timeLabel.textColor = UIColor.lightGray
        self.contentView.addSubview(timeLabel)
        
        contextLabel.font = UIFont.systemFont(ofSize: RATIO(maxNum: 16))
        contextLabel.numberOfLines = 2
        self.contentView.addSubview(contextLabel)
        
        button.backgroundColor = UIColor.pinkColor()
        button.setTitle(applyNow, for: .normal)
        button.setTitleColor(UIColor.white, for: .normal)
        self.contentView.addSubview(button)
        
        let lineView = UIView()
        lineView.backgroundColor = UIColor.lightGray.withAlphaComponent(0.5)
        self.contentView.addSubview(lineView)
        
        titleImageView.snp.makeConstraints { (make) in
            make.left.equalToSuperview().offset(RATIO(maxNum: 10))
            make.right.equalToSuperview().offset(RATIO(maxNum: -10))
            make.top.equalToSuperview().offset(RATIO_H(maxNum: 10))
            make.height.equalTo(RATIO_H(maxNum: 150))
        }
        
        titleLabel.snp.makeConstraints { (make) in
            make.left.equalTo(titleImageView)
            make.right.equalTo(titleImageView)
            make.top.equalTo(titleImageView.snp.bottom).offset(RATIO_H(maxNum: 5))
            make.height.equalTo(RATIO_H(maxNum: 20))
        }
        
        timeLabel.snp.makeConstraints { (make) in
            make.top.equalTo(titleLabel.snp.bottom).offset(RATIO_H(maxNum: 5))
            make.left.equalTo(titleLabel)
            make.right.equalTo(titleLabel)
            make.height.greaterThanOrEqualTo(10)
        }
        
        contextLabel.snp.makeConstraints { (make) in
            make.top.equalTo(timeLabel.snp.bottom).offset(RATIO_H(maxNum: 5))
            make.left.equalTo(timeLabel)
            make.right.equalTo(timeLabel)
            make.height.greaterThanOrEqualTo(10)
        }
        
        button.snp.makeConstraints { (make) in
            make.top.equalTo(contextLabel.snp.bottom).offset(RATIO_H(maxNum: 5))
            make.left.equalTo(contextLabel)
            make.right.equalTo(contextLabel)
            make.height.equalTo(RATIO_H(maxNum: 40))
        }
        
        lineView.snp.makeConstraints { (make) in
            make.left.right.bottom.equalToSuperview()
            make.height.equalTo(RATIO_H(maxNum: 1))
        }
    }
    
    
    //MARK: 更新数据
    fileprivate func updateData() {
        
        titleImageView.setUrlImage(with: kmHttps + model.image_url!, placeholder: UIImage.imageWithName("loading"))
        titleLabel.text = model.title
        if let time = model.time {
            timeLabel.text = time
        }else{
            timeLabel.text = ""
        }
        contextLabel.text = model.context
        
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    override func setSelected(_ selected: Bool, animated: Bool) {
        super.setSelected(selected, animated: animated)

        // Configure the view for the selected state
    }

}
